<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/2000/svg"
	xmlns:xlink="http://www.w3.org/1999/xlink"
	xmlns:doc="livcos.org/ns/doc"
	xmlns:dg="livcos.org/ns/doc/diagram"
	xmlns:mdl="livcos.org/ns/model"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:f="livcos.org/ns/xslt/function"
	exclude-result-prefixes="doc dg f xs mdl xlink">
	
	<xsl:import href="../../../style/base.xsl"/>
	
	<xsl:param name="ref"/>
	
	<xsl:template match="/">
		<xsl:apply-templates select="doc($ref)/*"/>
	</xsl:template>
	
	<xsl:template match="@* | node()">
		<xsl:copy>
			<xsl:apply-templates select="@* | node()"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="/*">
		<xsl:copy>
			<xsl:attribute name="id"><xsl:value-of select="f:object-id(.)"/></xsl:attribute>
			<xsl:apply-templates select="@* | node()"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="mdl:model">
	</xsl:template>
	
	<xsl:template match="dg:box">
		<xsl:variable name="id" select="substring(@ref, 2)"/>
		<xsl:variable name="type" select="//*[@id = $id]"/>
		<xsl:copy>
			<xsl:apply-templates select="@*"/>
			<xsl:apply-templates select="$type"/>
		</xsl:copy>
		<xsl:apply-templates select="$type/@specialize"/>
		<xsl:apply-templates select="$type/mdl:relation"/>
	</xsl:template>
	
	<xsl:template match="mdl:type/@specialize">
		<xsl:variable name="s" select="(//dg:box[@ref = concat('#',current()/../@id)])[1]"/>
		<xsl:variable name="e" select="(//dg:box[@ref = current()])[1]"/>
		<xsl:if test="$s/@id and $e/@id">
			<connect id="{../@id}" start="{$s/@id}" end="{$e/@id}" marker-end="arrow_3" xmlns="livcos.org/ns/doc/diagram"/>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="mdl:relation">
	</xsl:template>
	
	<xsl:template match="mdl:relation[mdl:end/@ref]">
		<xsl:variable name="s" select="(//dg:box[@ref = concat('#',current()/../@id)])[1]"/>
		<xsl:variable name="e" select="(//dg:box[@ref = current()/mdl:end/@ref])[1]"/>
		<xsl:variable name="p" select="(//dg:path[@ref = concat('#',current()/@id)])[1]"/>
		<xsl:if test="$s/@id and $e/@id">
			<connect id="{@id}" start="{$s/@id}" end="{$e/@id}" xmlns="livcos.org/ns/doc/diagram">
				<xsl:if test="not(mdl:start/@role)">
					<xsl:attribute name="marker-end">arrow_end_open</xsl:attribute>
				</xsl:if>
				<xsl:copy-of select="$p/*"/>
				<xsl:apply-templates select="mdl:start/@* | mdl:end/@*"/>
			</connect>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="mdl:start/@* | mdl:end/@*">
	</xsl:template>
	
	<xsl:template match="mdl:start/@role" priority="2">
		<text anchor="start" side="left" xmlns="livcos.org/ns/doc/diagram"><xsl:value-of select="."/></text>
	</xsl:template>
	
	<xsl:template match="mdl:end/@role" priority="2">
		<text anchor="end" side="left" xmlns="livcos.org/ns/doc/diagram"><xsl:value-of select="."/></text>
	</xsl:template>
	
	<xsl:template match="mdl:start/@cardinality" priority="2">
		<text anchor="start" side="right" xmlns="livcos.org/ns/doc/diagram"><xsl:value-of select="."/></text>
	</xsl:template>
	
	<xsl:template match="mdl:end/@cardinality" priority="2">
		<text anchor="end" side="right" xmlns="livcos.org/ns/doc/diagram"><xsl:value-of select="."/></text>
	</xsl:template>
	
	<xsl:template match="mdl:type">
		<xsl:variable name="w" select="string-length(@name) * 10 + 20"/>
		<xsl:variable name="h" select="count(mdl:property) * 14 + 24"/>
		<xsl:attribute name="width">
			<xsl:value-of select="$w"/>
		</xsl:attribute>
		<xsl:attribute name="height">
			<xsl:value-of select="$h"/>
		</xsl:attribute>
		<xsl:variable name="c" select="$w div 2, $h div 2"/>
		<g id="{@id}" fill="#CCDDEE">
			<a xlink:href="{@ref}">
				<rect stroke="#001122" x="0" y="0" width="{$w}" height="{$h}" rx="1" ry="1">
					<title><xsl:value-of select="@name"/></title>
					<desc><xsl:value-of select="@name"/></desc>
				</rect>
				<text x="{$c[1]}" y="16" fill="black" style="text-anchor: middle; font-size: 12px">
					<xsl:value-of select="@name"/>
				</text>
			</a>
			<xsl:if test="mdl:property">
				<line x1="0" y1="22" x2="{$w}" y2="22" stroke="#556677" stroke-width="1px"/>
				<rect x="1" y="22" width="{$w - 2}" height="{$h - 23}" fill="#EFF7FF"/>
				<g transform="translate(10,18)">
					<xsl:apply-templates select="mdl:property"/>
				</g>
			</xsl:if>
		</g>
	</xsl:template>
	
	<xsl:template match="mdl:property">
		<text x="0" y="{position() * 14}" fill="black" style="font-size: 10px">
			<xsl:value-of select="@name"/>
			<xsl:if test="@type">: <xsl:value-of select="@type"/></xsl:if>
		</text>
	</xsl:template>
	
</xsl:stylesheet>